package com.test.demo.main.adapter


import android.os.Build
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.test.demo.R
import com.test.demo.ext.loadUrl
import com.test.demo.ext.setImageUrl
import com.test.demo.main.bean.ItemTopicListBean
import com.test.demo.main.bean.MediumBean
import com.test.demo.utils.ViewPager2Activity
import com.test.demo.utils.glide.widget.SimpleDraweeView
import com.test.demo.view.CustomJZVideo
import com.test.demo.widget.EmojiTextView
import com.test.demo.widget.GridItemDecoration
import net.lucode.hackware.magicindicator.buildins.UIUtil


/**
 * @description:
 */
class TopicDetailsAdapter :
    BaseQuickAdapter<ItemTopicListBean, BaseViewHolder>(R.layout.adapter_item_original_pictext) {
    @RequiresApi(Build.VERSION_CODES.M)
    override fun convert(holder: BaseViewHolder, item: ItemTopicListBean) {
        val tvContent = holder.getView<EmojiTextView>(R.id.tv_Content)
        val tvHotComment = holder.getView<TextView>(R.id.tv_comment_content)
        val ivCircle = holder.getView<ImageView>(R.id.profile_circle)
        ivCircle.setImageResource(R.drawable.logo_head)

        val hotComment = context.getString(R.string.hot_comment)
        val spannableString = SpannableString(hotComment)
        val index = hotComment.indexOf(":")
        val numIndex = hotComment.lastIndexOf("8")
        spannableString.setSpan(
            ForegroundColorSpan(context.getColor(R.color.title_blue)),
            0,
            index,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        spannableString.setSpan(
            ForegroundColorSpan(context.getColor(R.color.title_blue)),
            numIndex,
            hotComment.length,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        tvHotComment.text = spannableString

        initImageList(item.urlList, holder)

    }


    var remotePlaybackWndSv: CustomJZVideo? = null

    private fun initImageList(urlList: List<MediumBean>?, holder: BaseViewHolder) {
        if (urlList?.size == 1) {
            if (urlList[0].type == 1) {
                setGoneLayout(holder, 1)
                val view = holder.getView<ConstraintLayout>(R.id.one_layout)
                val bigImage = view.findViewById<SimpleDraweeView>(R.id.iv_big_image)
                bigImage.loadUrl(urlList[0].resId!!)
                bigImage.setOnClickListener {
                    ViewPager2Activity.startNetUrl(context, urlList, 0)
                }

            } else {
                setGoneLayout(holder, 3)
                val view = holder.getView<ConstraintLayout>(R.id.video_layout)
                remotePlaybackWndSv =
                    view.findViewById<CustomJZVideo>(R.id.remote_playback_wnd_sv)
                remotePlaybackWndSv?.setUp(urlList[0].url, "")
                val shopMasterPicture =
                    urlList[0].url + "?x-oss-process=video/snapshot,t_0,f_jpg,w_0,h_0,m_fast,ar_auto"
                remotePlaybackWndSv?.posterImageView?.setImageUrl("${shopMasterPicture}")
                remotePlaybackWndSv?.posterImageView?.setOnClickListener {
                    onItem?.onclick(urlList[0].url)
                }
                remotePlaybackWndSv?.textureViewContainer?.setOnClickListener {
                    onItem?.onclick(urlList[0].url)
                }
            }
        } else {
            setGoneLayout(holder, 2)
            val twoLayout = holder.getView<LinearLayout>(R.id.two_layout)
            val twoRecyclerView = twoLayout.findViewById<RecyclerView>(R.id.two_recycler)
            var topicChildAdapter = TopicChildAdapter()
            topicChildAdapter.setList(urlList)
            twoRecyclerView.layoutManager = GridLayoutManager(context, 3)

            val divider: GridItemDecoration = GridItemDecoration.Builder(context)
                    .setHorizontalSpan(context.resources.getDimension(com.example.common_library.R.dimen.dp_5))
                    .setVerticalSpan(context.resources.getDimension(com.example.common_library.R.dimen.dp_5))
                    .setColorResource(R.color.white)
                    .setShowLastLine(true)
                    .build()

            twoRecyclerView.addItemDecoration(divider)

            twoRecyclerView.adapter = topicChildAdapter
            topicChildAdapter.setOnItemClickListener(object : OnItemClickListener {
                override fun onItemClick(
                    adapter: BaseQuickAdapter<*, *>,
                    view: View,
                    position: Int
                ) {
                    ViewPager2Activity.startNetUrl(context, urlList, position)
                }

            })
        }
    }

    /**
     * 设置竖向大图片
     */
    private fun setVerticalView(bigImage: SimpleDraweeView, width: Int, height: Int, url: String?) {
        bigImage.layoutParams.width = UIUtil.dip2px(context, 150.0)
        bigImage.layoutParams.height = UIUtil.dip2px(context, 300.0)
        bigImage.scaleType = ImageView.ScaleType.CENTER_CROP
        bigImage.layoutParams = bigImage.layoutParams
        bigImage.loadUrl(url)
    }

    private fun setVerticalViewres(bigImage: SimpleDraweeView, width: Int, height: Int, res: Int) {
        bigImage.layoutParams.width = UIUtil.dip2px(context, 150.0)
        bigImage.layoutParams.height = UIUtil.dip2px(context, 300.0)
        bigImage.scaleType = ImageView.ScaleType.CENTER_CROP
        bigImage.layoutParams = bigImage.layoutParams
        bigImage.setImageResource(res)
    }

    /**
     * 判断图片尺寸是横向还是竖向
     */
    private fun getHorizontalvertical(width: Int, height: Int): Boolean {
        return (width > height)
    }

    fun getImageAspectRatio(width: Int, height: Int): Float {
        return width.toFloat() / height.toFloat()
    }

    private fun setGoneLayout(holder: BaseViewHolder, showType: Int) {
        when (showType) {
            1 -> {
                holder.setGone(R.id.one_layout, false)
                holder.setGone(R.id.two_layout, true)
                holder.setGone(R.id.video_layout, true)
            }
            2 -> {
                holder.setGone(R.id.one_layout, true)
                holder.setGone(R.id.two_layout, false)
                holder.setGone(R.id.video_layout, true)
            }
            3 -> {
                holder.setGone(R.id.one_layout, true)
                holder.setGone(R.id.two_layout, true)
                holder.setGone(R.id.video_layout, false)
            }
        }
    }

    interface onItemRemotePlay {
        fun onclick(url: String?)
    }

    var onItem: onItemRemotePlay? = null

    fun setItem(onItem1: onItemRemotePlay) {
        this.onItem = onItem1
    }

    fun getGSYVideoPlayer(): CustomJZVideo? {
        return remotePlaybackWndSv
    }

}